﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerNhomTuoiDao : INhomTuoiDao
    {
        public SqlServerNhomTuoiDao()
        {
            SQLUtility.TableName = "NhomTuoi";
        }

        //Trả về đối tượng báo ăn
        private NhomTuoi MakeNhomTuoi(DataRow row)
        {
            int _id = int.Parse(row["Id"].ToString());
            string _tenNhomTuoi = row["TenNhomTuoi"].ToString();
              return new NhomTuoi(_id, _tenNhomTuoi);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<NhomTuoi> MakeNhomTuois(DataTable dt)  
        {
            IList<NhomTuoi> list = new List<NhomTuoi>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeNhomTuoi(row));
            }
            return list;
        }
        private DbParameter[] Take(NhomTuoi NhomTuoi)
        {
            DbParameter[] parameters = new DbParameter[2];
            parameters[0] = new SqlParameter("@Id", NhomTuoi.Id);
            parameters[1] = new SqlParameter("@TenNhomTuoi", NhomTuoi.TenNhomTuoi);
            return parameters;
        }
        public IList<Business_Objects.NhomTuoi> GetNhomTuoi()
        {
            SQLUtility.TableName = "NhomTuoi";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeNhomTuois(dt); 
        }

        public Business_Objects.NhomTuoi GetNhomTuoiByID(string  NhomTuoiID)
        {
            SQLUtility.TableName = "NhomTuoi";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@Id", NhomTuoiID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeNhomTuoi(row);
            }
        }

        public void InsertNhomTuoi(Business_Objects.NhomTuoi NhomTuoi)
        {
            SQLUtility.TableName = "NhomTuoi";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(NhomTuoi), true);
        }

        public int UpdateNhomTuoi(Business_Objects.NhomTuoi NhomTuoi)
        {
            SQLUtility.TableName = "NhomTuoi";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(NhomTuoi)); }
            catch { return -1; }
        }

        public int DeleteNhomTuoi(Business_Objects.NhomTuoi NhomTuoi)
        {
            SQLUtility.TableName = "NhomTuoi";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(NhomTuoi)); }
            catch { return -1; }
        }
    }
}
